package com.onesports.intelligent.k12.polarlight.service.course.manage;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.intelligent.k12.polarlight.domain.dto.course.CourseClassifySaveOrUpdateDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.CourseClassify;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.CourseSubLevel;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.CourseClassifyVO;
import com.onesports.intelligent.k12.polarlight.mapper.course.CourseClassifyMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author FPH
 * @since 2022年7月8日10:45:22
 * 课程分类
 */
@Service
@RequiredArgsConstructor
public class CourseClassifyService extends ServiceImpl<CourseClassifyMapper, CourseClassify> {

    private final CourseClassifyMapper courseClassifyMapper;
    /**
     * 获取课程列表下拉
     * @return
     */
    public List<CourseClassifyVO> queryClassifyNameList(){
        List<CourseClassify> list = list();
        return list.stream().map(i-> CourseClassifyVO.builder()
                        .id(i.getId())
                        .classifyName(i.getClassifyName())
                        .classifyCode(i.getClassifyCode())
                        .build())
                .collect(Collectors.toList());
    }

    /**
     * 校验名称是否重复
     * @param name
     * @return
     */
    public Boolean checkName(String name,String id){
        List<CourseClassify> list=list(new LambdaQueryWrapper<CourseClassify>().eq(CourseClassify::getClassifyName, name));
        if(CollectionUtils.isNotEmpty(list)){
            if(StringUtils.isNotEmpty(id)){
                List<CourseClassify> collect = list.stream().filter(i -> !id.equals(i.getId())).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(collect)){
                    return false;
                }else {
                    return true;
                }
            }
            return false;
        }else{
            return true;
        }
    }


    /**
     * 新增修改通用ID鉴别
     * @param dto
     * @return
     */
    public CourseClassifySaveOrUpdateDTO checkId(CourseClassifySaveOrUpdateDTO dto){
        if(StringUtils.isEmpty(dto.getId())){
            return dto;
        }
        CourseClassify courseClassify = getById(dto.getId());
        if(courseClassify!=null){
            return dto;
        }
        dto.setId(null);
        return dto;
    }


    /**
     * 获取classCode集合
     * @return
     */
    public List<String> getClassCode(){
        return list().stream().map(CourseClassify::getClassifyCode).collect(Collectors.toList());
    }

    /**
     * 根据id获取分阶集合
     * @param id
     * @return
     */
    public List<CourseSubLevel> queryCourseSubLevelById(String id){
        return courseClassifyMapper.queryCourseSubLevelById(id);
    }

    /**
     * 根据name返回其ID
     * @param name
     * @return
     */
    public String queryIDListByName(String name){
        return  list(new LambdaQueryWrapper<CourseClassify>().eq(CourseClassify::getClassifyName, name)).stream()
                .map(CourseClassify::getId).findFirst().orElse(null);
    }
}
